package com.live.adapter;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.live.activity.LiveFriendHomeActivity;
import com.live.activity.R;
import com.live.common.Constant;
import com.live.common.ImageDownloader;
import com.live.task.LiveTopic;

public class LiveTopicSA extends SimpleAdapter {

	public List<Map<String, Object>> items;
	public Context context;
	public String uId;
	public int index = 0;

	private final ImageDownloader imageDownloader = new ImageDownloader();

	public LiveTopicSA(Context context, List<Map<String, Object>> items, int resource, String[] from, int[] to,
			String uId) {
		super(context, items, resource, from, to);
		this.items = items;
		this.context = context;
		this.uId = uId;
	}

	@Override
	@SuppressWarnings("unchecked")
	public View getView(final int position, View convertView, ViewGroup parent) {
		final Map<String, Object> obj = (Map<String, Object>) getItem(position);

		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.topic_list, null, true);
		}

		TextView content = (TextView) convertView.findViewById(R.id.content);
		if (obj.get("replayId") != null && !obj.get("userId").equals(obj.get("atUserId"))) {
			content.setText(Html.fromHtml("<font color='#2D86B0'>" + obj.get("userId") + "</font>" + " 回复 "
					+ "<font color='#2D86B0'>" + obj.get("atUserId") + "</font> " + obj.get("content")));
		} else {
			content.setText(Html.fromHtml("<font color='#2D86B0'>" + obj.get("userId") + "</font> "
					+ obj.get("content")));
		}

		// 设置性别
		TextView userSex = (TextView) convertView.findViewById(R.id.userSex);
		if (Constant.USER_SEX_MAN.equals(obj.get("userSex"))) {
			userSex.setText("♂");
			userSex.setTextColor(0xff00CCFF);
		} else {
			userSex.setText("♀");
			userSex.setTextColor(0xffFF6699);
		}

		// 设置城市
		if (!"null".equals(obj.get("userCity").toString())) {
			TextView userCity = (TextView) convertView.findViewById(R.id.userCity);
			userCity.setText((String) obj.get("userCity"));
		}

		// 设置年龄
		TextView userAge = (TextView) convertView.findViewById(R.id.userAge);
		userAge.setText((String) obj.get("userAge"));

		// 设置日期
		TextView reviewDate = (TextView) convertView.findViewById(R.id.topicDate);
		reviewDate.setText((String) obj.get("updateDate"));

		// 设置头像
		ImageView userPic = (ImageView) convertView.findViewById(R.id.userPic);
		if (Constant.Y.equals(obj.get("userPic"))) {
			String imageUrl = Constant.SITE_URL + Constant.SITE_AVATAR_PATH + obj.get("uId") + ".jpg";
			imageDownloader.download(imageUrl, userPic, (String) obj.get("userPicVer"));
		} else {
			userPic.setImageResource(R.drawable.default_pic);
		}
		userPic.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// 当不是本人的时候
				if (uId == null || !uId.equals((String) obj.get("uId"))) {
					Intent intent = new Intent();
					intent.setClass(context, LiveFriendHomeActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("f", (String) obj.get("f"));
					bundle.putString("uId", (String) obj.get("uId"));
					bundle.putString("userId", (String) obj.get("userId"));
					bundle.putString("userSign", (String) obj.get("userSign"));
					bundle.putString("userSex", (String) obj.get("userSex"));
					bundle.putString("userAge", (String) obj.get("userAge"));
					bundle.putString("userCity", (String) obj.get("userCity"));
					bundle.putString("userPic", (String) obj.get("userPic"));
					bundle.putString("userPicVer", (String) obj.get("userPicVer"));
					intent.putExtras(bundle);
					context.startActivity(intent);
				}
			}
		});

		// 设置删除按钮
		ImageView delBtn = (ImageView) convertView.findViewById(R.id.delBtn);
		if (obj.get("uId").equals(uId)) {
			delBtn.setVisibility(View.VISIBLE);
			// 删除评论事件监听
			delBtn.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {

					// 生成确认对话框
					AlertDialog.Builder builder = new AlertDialog.Builder(context);
					builder.setTitle(R.string.delete_confirm_info);
					builder.setIcon(android.R.drawable.ic_dialog_info);
					builder.setPositiveButton(R.string.delete_btn, new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

							Bundle bundle = new Bundle();
							bundle.putString("rId", (String) obj.get("id"));
							bundle.putInt("index", position);

							// 删除评论
							LiveTopic task = new LiveTopic(context, items, LiveTopicSA.this, null,
									Constant.ACTION_DELETE, null, null);
							task.execute(bundle);

						}
					});
					builder.setNegativeButton(R.string.cancel_btn, new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
						}
					});
					builder.create().show();

				}
			});
			delBtn.setVisibility(View.VISIBLE);
		} else {
			delBtn.setVisibility(View.INVISIBLE);
		}
		return convertView;
	}
}